package com.qqtech.qquan.mall.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.mall.dao.MallCategoryDao;
import com.qqtech.qquan.mall.model.*;

@Service
public class MallCategoryServiceImpl extends BaseServiceImpl<MallCategory>
		implements MallCategoryService {
	public static final Logger log = LoggerFactory.getLogger(MallCategoryServiceImpl.class);
	
	@Resource
	private MallCategoryDao mallCategoryDao;

	@Override
	protected BaseDao<MallCategory> getBaseDao() {
		return mallCategoryDao;
	}
	
	@Override
	public Integer getMaxOidxByParentId(String parentId) {
		if (StringUtil.isBlank(parentId)) {
			parentId = "0";
		}
		MallCategoryQuery mallCategoryQuery = new MallCategoryQuery();
		mallCategoryQuery.setParentId(parentId);
		Integer maxOidx = mallCategoryDao.getMaxOidx(mallCategoryQuery);
		if (null == maxOidx) {
			maxOidx = 0;
		}
		return maxOidx;
	}

	@Override
	public List<MallCategoryVo> queryListByParentId(String parentId,Integer status) {
		if (StringUtil.isBlank(parentId)) {
			parentId = "0";
		}
		MallCategoryQuery mallCategoryQuery = new MallCategoryQuery();
		mallCategoryQuery.setParentId(parentId);
		mallCategoryQuery.setStatus(status);
		return mallCategoryDao.queryList(mallCategoryQuery);
	}

	@Override
	public List<MallCategoryVo> queryFirstLevelList(Integer status) {
		MallCategoryQuery mallCategoryQuery = new MallCategoryQuery();
		mallCategoryQuery.setParentId("0");
		mallCategoryQuery.setStatus(status);
		Pageable pageKit = new PageKit(mallCategoryQuery.getPage(),900, new PageOrder(Direction.ASC, "oidx"));
		return mallCategoryDao.queryList(mallCategoryQuery, pageKit);
	}

	/**
	 * 获取categoryId的所有上级分类
	 * @param categoryId
	 * @return
	 */
	@Override
	public List<MallCategoryVo> findAllParent(String categoryId) {
		if (StringUtil.isBlank(categoryId)) {
			return null;
		}
		return mallCategoryDao.findAllParent(categoryId);
	}

	/**
	 * 根据分类id获取
	 * @param categoryId 分类id
	 * @return
	 */
	@Override
	public MallCategoryVo getByCategoryId(String categoryId) {
		return mallCategoryDao.getByCategoryId(categoryId);
	}

	/**
	 * 更新
	 * @param entity
	 * @return
	 */
	@Override
	public int updateByCategoryId(MallCategory entity) {
		return mallCategoryDao.updateByCategoryId(entity);
	}
	
}